<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 20px">
      <el-breadcrumb-item :to="{ path: '/' }">销售管理</el-breadcrumb-item>
      <el-breadcrumb-item>销售制单</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>


    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="销售单号" prop="salesNumber">
        <el-input style="width: 300px" v-model="ruleForm.salesNumber"></el-input>
      </el-form-item>

      <el-form-item label="客户" prop="customerId">
        <el-select style="width: 300px" v-model="ruleForm.customerId" placeholder="请选择">
          <el-option
              v-for="item in customerListOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="业务员" prop="userId">
        <el-select style="width: 300px" v-model="ruleForm.userId" placeholder="请选择">
          <el-option
              v-for="item in userListOptions"
              :key="item.id"
              :label="item.username"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商品编号" prop="productId">
        <el-input style="width: 300px" v-model="ruleForm.productId"></el-input>
      </el-form-item>
      <el-form-item label="数量" prop="count">
        <el-input style="width: 300px" v-model="ruleForm.count"></el-input>
      </el-form-item>
      <el-form-item label="价格" prop="salesPrice">
        <el-input style="width: 300px" v-model="ruleForm.salesPrice"></el-input>
      </el-form-item>

      <el-button style="margin-left: 100px" type="primary" @click="submitForm('ruleForm')">生成销售单</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form>
  </div>
</template>
<script>
export default {
  data() {
    return {
      customerListOptions:[],
      userListOptions:[],
      ruleForm: {
        salesNumber:'',
        productId:'',
        count:'',
        salesPrice:'',
        userId:'',
        customerId:'',
      },
      rules: {
        salesNumber: [
          {required: true, message: '请输入销售单号', trigger: 'blur'},
        ],
        productId: [
          {required: true, message: '请输入商品编号', trigger: 'blur'},
        ],
        count: [
          {required: true, message: '请输入销售数量', trigger: 'blur'},
          {min: 2, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur'}
        ],
        salesPrice: [
          {required: true, message: '请输入销售价格', trigger: 'blur'},
        ],
        userId: [
          {required: true, message: '请输入业务员', trigger: 'blur'},
        ],
      }
    }
  },

  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:8080/sales/add-new';
          let formData = this.qs.stringify(this.ruleForm, {arrayFormat: 'repeat'});
          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .post(url, formData).then((response) => {
            let responseBody = response.data;
            if (responseBody.state == 20000) {
              this.$message({
                message: '添加客户成功！',
                type: 'success'
              });
              this.resetForm(formName);
            } else {
              this.$message.error(responseBody.message);
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },

    loadCustomerList() {
      let url = 'http://localhost:8080/customers/list';
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.customerListOptions = responseBody.data;
        } else {
          this.$message.error(responseBody.message);
        }
      });
    },
    loadUserList() {
      let url = 'http://localhost:8080/sales/select';
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.userListOptions = responseBody.data;
          console.log(this.userListOptions)
        } else {
          this.$message.error(responseBody.message);
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  mounted() {
    this.loadCustomerList();
    this.loadUserList();
  }
}
</script>